import { Component, Input, OnInit } from '@angular/core';
import { OrdOrder, OrdOrderCondition } from '../../ord-order.type';
import { ActivatedRoute, Router } from '@angular/router';
import { Services } from '../../../../../frame/module/services';
import { ErrRes } from '../../../../../frame/global/err-res';
import { isNullOrUndefined } from 'util';
import { UrlRou } from '../../../../../frame';
import { ModifyOrdOrderComponent } from '../modify-ord-order.component';
import { Zero3Enum } from 'src/app/frame/enum/zero3-enum';
import { OrdOrderLogistics } from '../../../ord-order-logistics/ord-order-logistics.type';

@Component({
    selector: 'app-main-modify-ord-order',
    templateUrl: './main-modify-ord-order.component.html',
    styleUrls: ['./main-modify-ord-order.component.css']
})
export class MainModifyOrdOrderComponent implements OnInit {

    errRes = new ErrRes();

    zero3Enum = Zero3Enum;

    domain: OrdOrder = new OrdOrder();

    orderLogistics: OrdOrderLogistics = new OrdOrderLogistics();

    orderLines: any[];

    extend = {
        domain: {},
        orderStatus: () => {
            return Zero3Enum.ORDER_STATUS_FLAG.get(this.domain['serOrderStatus']);
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: OrdOrderCondition = new OrdOrderCondition();
            condition.uuid = uuid;
            Services.ordOrder.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    console.log(item.domain);
                }
            });
            Services.ordOrderLine.findBySerOrderId(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domains)) {
                    this.orderLines = item.domains;
                    console.log(item.domain);
                }
            });
            Services.ordOrderLogistics.getByOrderUUID(uuid).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    console.log(item.domain);
                    this.orderLogistics = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.ordOrder.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.ordOrder.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyOrdOrder: ModifyOrdOrderComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyOrdOrder.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
